package com.ruoyi.order.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.order.utils.OrdersData;
import com.ruoyi.order.utils.RedisIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.Orders;
import com.ruoyi.order.service.IOrdersService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController {

    @Autowired
    private IOrdersService ordersService;
    /**
     * 查询订单信息列表
     */

    // 添加维修订单
    @PostMapping("/weixiu/addOrder")
    public AjaxResult addOrder(@RequestBody Orders orders) {
        System.out.println("订单信息：" + orders);
        if (orders == null) {
            return AjaxResult.error("订单信息为空");
        }
        return ordersService.addWeixiuOrder(orders);
    }

    // 添加汽车保养订单
    @PostMapping("/baoyang/addOrder")
    public AjaxResult addBaoYangOrder(@RequestBody Orders orders) {
        //TODO 生成订单号
        return null;
    }

    // 添加洗车订单
    @PostMapping("/xiche/addOrder")
    public AjaxResult addXiCheOrder(@RequestBody Orders orders) {
        //TODO 生成订单号
        return null;
    }

    // 添加救援订单
    @PostMapping("/yujue/addOrder")
    public AjaxResult addYuJueOrder(@RequestBody Orders orders) {
        //TODO 生成订单号
        return null;
    }

//    @RequiresPermissions("order:orders:list")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders) {
        System.out.println(orders);
        startPage();
        System.out.println("开始查询订单信息列表");
        List<OrdersData> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @RequiresPermissions("order:orders:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders) {
        List<OrdersData> list = ordersService.selectOrdersList(orders);
        ExcelUtil<OrdersData> util = new ExcelUtil<>(OrdersData.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @RequiresPermissions("order:orders:query")
    @GetMapping(value = "/{oId}")
    public AjaxResult getInfo(@PathVariable("oId") Long oId)
    {
        return success(ordersService.selectOrdersByOId(oId));
    }

    /**
     * 新增订单信息
     */
    @RequiresPermissions("order:orders:add")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单信息
     */
//    @RequiresPermissions("order:orders:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping("/xiugai")
    public AjaxResult edit(@RequestBody Orders orders)
    {
        int updateOrders = ordersService.updateOrders(orders);
        if (updateOrders > 0) {
            return success("操作成功");
        }else {
            return error("更新失败，服务器繁忙，请稍后再试");
        }
    }

    /**
     * 删除订单信息
     */
    @RequiresPermissions("order:orders:remove")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{oIds}")
    public AjaxResult remove(@PathVariable Long[] oIds)
    {
        return toAjax(ordersService.deleteOrdersByOIds(oIds));
    }


}
